<?php
/**
 * JoiPlugins - библиотека для работы с плагинами
 *
 * Core Class
 *
 * @version 1.0 
 * @package JoiPlugins
 * @filename joiPlugins.class.php
 * @author JoostinaTeam
 * @copyright (C) 2008-2009 Joostina Team
 * @license GNU GPL 2.0. Пожалуйста, не удаляйте копирайты из исходного кода. Спасибо. 
 *
 **/
 DEFINE('JOIPLUGINS', '1');
class joiPlugins {
	
    var $component = null; //group
    var $plugin = null; //subgroup
    var $config = null;
    var $type = null;
    var $object = null;
    var $object_params = null;
    var $_mainframe = null;
	
	function joiPlugins($mainframe, $component, $plugin = null, $type = null){        
        $this->_mainframe = $mainframe;
        $this->component = $component;

        $config = new joiPluginsConfig($this->_mainframe->_db, $this->component.'_plugin', $plugin, $type); 
        
        if($type){
            $this->type = $type;    
        } 
                
        if($plugin){
            $config->_subgroup = $plugin;                
        }
        
        $this->config = $config;
              	
	}

    function init($type = '', $object = null, $object_params = null){
        if($type){
            $this->type = $type;    
        } 
        $this->object = $object;
        $this->object_params = $object_params;   
    }
    
    function load($plugin){

        $this->plugin = $plugin;
        $this->config->_subgroup = $plugin;
                
	$plugin_file = $this->_mainframe->getCfg('absolute_path').DS.'components'.DS.'com_joigallery'.DS.'plugins'.DS;
        if($this->type){
            $plugin_file .= $this->type.DS;  
        }
        $plugin_file .= $plugin.DS.strtolower($plugin).'.php';  

	   if(is_file($plugin_file)){
            
              require_once($plugin_file);

              $pluginClass_c = $this->config->_subgroup.'Config';
              $pluginClass_c = new $pluginClass_c($this->plugin);

              if($this->config->_all_configs && key_exists($plugin, $this->config->_all_configs)){
                  $pluginClass_c = $this->config->bind_plugin_config($pluginClass_c);
              }  

              $pluginClass = new $this->plugin($this, $pluginClass_c);

              $pluginClass->show_content();
	    }
	    else{
			return false;
		}       
    }
    
    function get_plugins_list(){
        $plugins_list = array();

        $dir_list_types = scandir(JPATH_BASE.DS.'components'.DS.'com_joigallery'.DS.'plugins');
        
        foreach($dir_list_types as $type){
            $dir = JPATH_BASE.DS.'components'.DS.'com_joigallery'.DS.'plugins'.DS.$type;
            if( is_dir($dir)  && $type!=='.' && $type!=='..' && $type!=='.svn'){
                $dir_list_plugins = scandir($dir);
                foreach($dir_list_plugins as $plugin){
                    $dir_plugin = $dir.DS.$plugin;
                    if( is_dir($dir_plugin)  && $plugin!=='.' && $plugin!=='..' && $plugin!=='.svn'){
                        //echo $plugin.' - '.$type.'<br />';
                        $plugins_list[$type][] = $plugin;
                        
                    }
                }                
            }
        }
        return $plugins_list;
        
    }

}

class joiPluginsConfig extends DBconfig{    

    var $_all_configs = null; 
    var $_plugin_type = null;
    var $_plugin = null;  
    var $_error = null; 
    
   	function joiPluginsConfig(&$db, $group = '', $subgroup = '', $plugin_type = null) {

       $db = database::getinstance();
       $this->_db = $db;
       //$this->_group = $group;       
       
       if($plugin_type){
        $this->_plugin_type = $plugin_type;
       }
       
       //все плагины
       if($subgroup){
        $this->_all_configs = $this->get_config_array(parent::__construct($db, $group, $subgroup));
       }
       else{
        $this->_all_configs = $this->get_config_array(parent::__construct($db, $group));
       }
       
       
       if($subgroup){
        $this->_plugin = $subgroup;
       }
     	

	}
    
    function get_config_array($all_configs){
		$plugins_array = array();
        
        if($all_configs){
    		foreach($all_configs as $v){
    		  $plugins_array[$v->subgroup][] = $v;
    		}
            
            return $plugins_array;              
        }
        else{
            return false;
        }
		
      
    }
    
	function bind_plugin_config($object){
	   
        $array = $this->_all_configs;
        
		$binds = array();

        foreach ($array[$this->_subgroup] as $v){
            $binds[$v->name] = $this->_parseValue($v->value) ;
        }
        
		$rows = get_object_vars($object);
		foreach ($rows as $key => $value) {
			if (isset($binds[$key]) and substr($key, 0, 1) !== '_') {
				$object->$key = $binds[$key];
                //echo $key .' - ' .$binds[$key];
			}
		}
		
		return $object;	
	}
    
	function save_config($option) {
	   
        $mainframe = &mosMainFrame::getInstance();
                
		$plugin_file = JPATH_BASE.DS.'components'.DS.$option.DS.'plugins'.DS;
        if($this->_plugin_type){
            $plugin_file .= $this->_plugin_type.DS;  
        }
        $plugin_file .= $this->_subgroup.DS.strtolower($this->_subgroup).'.php';  

		if(is_file($plugin_file)){
            
            require_once($plugin_file);
            $pluginClass_c = $this->_subgroup.'Config'; 
  
            $pluginClass_c = new $pluginClass_c($this->_plugin);
            $class_vars = get_class_vars(get_class($pluginClass_c)); 
            foreach ($class_vars as $name => $value) {
		      $this->$name = $pluginClass_c->$name;
    		} 
		}
		else{
			return false;
		}  
		
		if(!$this->bindConfig($_REQUEST)) {
			echo "<script> alert('".$this->_error."'); window.history.go(-1); </script>";
			exit();
		}
		
		if(!$this->storeConfig()) {
			echo "<script> alert('".$this->_error."'); window.history.go(-1); </script>";
			exit();
		}
	}
    
	function display_config($option) {
        $mainframe = &mosMainFrame::getInstance();
                
	$plugin_file = $config_file = JPATH_BASE.DS.'components'.DS.$option.DS.'plugins'.DS;
                
        if($this->_plugin_type){
            $plugin_file .= $this->_plugin_type.DS; 
            $config_file .= $this->_plugin_type.DS; 
        }
        $plugin_file .= $this->_subgroup.DS.strtolower($this->_subgroup).'.php';
        $config_file .=  $this->_subgroup.DS.'display_config.php';       
        
        if(!is_file($plugin_file)){
            return $this->error_page('Файл плагина не найден (путь к файлу: '.$plugin_file.')');
        }
        if(!is_file($config_file)){
            return $this->error_page('Плагин не содержит настроек');
        }

            
        require_once($plugin_file);
        $pluginClass_c = $this->_subgroup.'Config'; 

        $pluginClass_c = new $pluginClass_c($this->_plugin); 
        
        if($this->_all_configs){
            $pluginClass_c = $this->bind_plugin_config($pluginClass_c);     
        }                  
        
        $pluginClass_c->display_config();  



	}
    
    function error_page($msg = ''){
        ?>
        <table class="adminheading">
          <tr>
            <th class="config">
             <?php echo _C_JOIGALLERY_COMPONENT;
                   echo _C_JOIGALLERY_PLUGIN.'&nbsp;['.$this->_plugin.']';
              ?>
            </th>
          </tr>
        </table>
        <form action="index2.php" method="post" name="adminForm" enctype="multipart/form-data">

          <?php echo $msg;?>

          <input type="hidden" name="option" value="com_joigallery" />
          <input type="hidden" name="task" value="" />
          <input type="hidden" name="act" value="plugins" />
          <input type="hidden" name="plugin" value="<?php echo $this->_plugin; ?>" />
          <input type="hidden" name="type" value="<?php echo $this->_plugin_type; ?>" />
          <input type="hidden" name="hidemainmenu" value="0" />
          
        </form>
        <?php    
    }

}